﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.Project;
using ZFine.Repository.Project;


namespace ZFine.Application.Project
{
    public class InstructionsApp
    {
        private IInstructionsRepository service = new InstructionsRepository();

        /// <summary>
        /// 查全表
        /// </summary>
        /// <returns></returns>
        public List<InstructionsEntity> GetList()
        {
            return service.IQueryable().ToList();
        }

        public InstructionsEntity getdefaultobj(Expression<Func<InstructionsEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).FirstOrDefault();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public InstructionsEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValue"></param>

        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        #region 操作
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<InstructionsEntity> GetListC(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<InstructionsEntity>();
            expression = expression.And(t => t.State == true);
            expression = expression.And(t => t.Type == 1);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.Title.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        /// <summary>
        /// 操作提交
        /// </summary>
        /// <param name="NewsArticle"></param>
        /// <param name="keyValue"></param>
        public int SubmitFormC(InstructionsEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    if (ServiceEntity.AddUserName == null)
                    {
                        ServiceEntity.AddUserName = LoginInfo.UserName;
                    }
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 1;
                    if (ServiceEntity.AddUserName == null)
                    {
                        ServiceEntity.AddUserName = LoginInfo.UserName;
                    }
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }
        #endregion

        #region 申请
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<InstructionsEntity> GetListH(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<InstructionsEntity>();
            expression = expression.And(t => t.State == true);
            expression = expression.And(t => t.Type == 2);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.Title.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        /// <summary>
        /// 申请提交
        /// </summary>
        /// <param name="NewsArticle"></param>
        /// <param name="keyValue"></param>
        public int SubmitFormH(InstructionsEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    if (ServiceEntity.AddUserName == null)
                    {
                        ServiceEntity.AddUserName = LoginInfo.UserName;
                    }
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 2;
                    if (ServiceEntity.AddUserName == null)
                    {
                        ServiceEntity.AddUserName = LoginInfo.UserName;
                    }
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }
        #endregion
    }
}
